package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.wms.entity.JobExpect;
import cn.stylefeng.guns.modular.wms.mapper.JobExpectMapper;
import cn.stylefeng.guns.modular.wms.model.params.JobExpectParam;
import cn.stylefeng.guns.modular.wms.model.result.JobExpectResult;
import  cn.stylefeng.guns.modular.wms.service.JobExpectService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 个人简历表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-04-02
 */
@Service
public class JobExpectServiceImpl extends ServiceImpl<JobExpectMapper, JobExpect> implements JobExpectService {

    @Override
    public void add(JobExpectParam param){
        JobExpect entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(JobExpectParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(JobExpectParam param){
        //判断ID是否存在，如果不存在，则新建
        if (param.getId()!=null){
            JobExpect oldEntity = getOldEntity(param);
            JobExpect newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public JobExpectResult findBySpec(JobExpectParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<JobExpectResult> findListBySpec(JobExpectParam param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(JobExpectParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(JobExpectParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private JobExpect getOldEntity(JobExpectParam param) {
        return this.getById(getKey(param));
    }

    private JobExpect getEntity(JobExpectParam param) {
        JobExpect entity = new JobExpect();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
